// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <melon/range.h>
#include "melon/io/io_buf.h"
#include <pollux/common/exception/exception.h>

namespace kumo::pollux::dwio::common::encryption {
    enum class EncryptionProvider { Unknown = 0, CryptoService };

    class EncryptionProperties {
    public:
        virtual ~EncryptionProperties() = default;

        virtual size_t hash() const = 0;

    protected:
        virtual bool equals(const EncryptionProperties &other) const = 0;

        friend bool operator==(
            const EncryptionProperties &,
            const EncryptionProperties &);
    };

    bool operator==(const EncryptionProperties &a, const EncryptionProperties &b);

    struct EncryptionPropertiesHash {
        std::size_t operator()(const EncryptionProperties *ep) const {
            return ep->hash();
        }
    };

    struct EncryptionPropertiesEqual {
        bool operator()(const EncryptionProperties *a, const EncryptionProperties *b)
        const {
            return *a == *b;
        }
    };

    class Encrypter {
    public:
        virtual ~Encrypter() = default;

        virtual const std::string &getKey() const = 0;

        virtual std::unique_ptr<melon::IOBuf> encrypt(
            melon::StringPiece input) const = 0;

        virtual std::unique_ptr<Encrypter> clone() const = 0;
    };

    class EncrypterFactory {
    public:
        virtual ~EncrypterFactory() = default;

        virtual std::unique_ptr<Encrypter> create(
            EncryptionProvider provider,
            const EncryptionProperties &props) = 0;
    };

    class Decrypter {
    public:
        virtual ~Decrypter() = default;

        virtual void setKey(const std::string &key) = 0;

        virtual bool isKeyLoaded() const = 0;

        virtual std::unique_ptr<melon::IOBuf> decrypt(
            melon::StringPiece input) const = 0;

        virtual std::unique_ptr<Decrypter> clone() const = 0;
    };

    class DecrypterFactory {
    public:
        virtual ~DecrypterFactory() = default;

        virtual std::unique_ptr<Decrypter> create(EncryptionProvider provider) = 0;
    };

    class DummyDecrypter : public Decrypter {
    public:
        void setKey(const std::string & /* unused */) override {
        }

        bool isKeyLoaded() const override {
            return false;
        }

        std::unique_ptr<melon::IOBuf> decrypt(
            melon::StringPiece /* unused */) const override {
            DWIO_RAISE("Failed to access encrypted data");
        }

        std::unique_ptr<Decrypter> clone() const override {
            return std::make_unique<DummyDecrypter>();
        }
    };

    // Fallback decypter factory used when file is encrypted and user doesn't
    // supply one. This allows unencrypted columns to be read from encrypted file
    // without factory
    class DummyDecrypterFactory : public DecrypterFactory {
    public:
        std::unique_ptr<Decrypter> create(EncryptionProvider /* unused */) override {
            return std::make_unique<DummyDecrypter>();
        }
    };
} // namespace kumo::pollux::dwio::common::encryption
